﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace studentmanagementdemo
{
    public partial class frmclass : Form
    {
        private SqlConnection con;
        private DataTable dtClass = new DataTable("tblClass");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;

        private void connect()
        {
            String cn = @"Data Source=(localdb)\linh123;Initial Catalog=iPMAC;Integrated Security=True";
            try
            {
                con = new SqlConnection(cn);
                con.Open(); //Moi ket noi                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối tới cơ sở dữ liệu !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void disconnect()
        {
            con.Close(); //Dong ket noi
            con.Dispose(); //Giai phong tai nguyen
            con = null; //Huy doi tuong
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand(); //Khai bao 1 command
            command.Connection = con; //Ket noi
            command.CommandType = CommandType.Text; //Khai bao kieu command
            command.CommandText = @"Select fldID as N'MaLop',
                                           fldClassName as N'TenLop',
                                           fldTime as N'CaHoc',
                                           fldCapacity as N'SiSo'
                                    from tblClass";
            da.SelectCommand = command; //Gan command cho da            
            da.Fill(dtClass); //Nap du lieu vao table
            grvclass.DataSource = dtClass; //Load du lieu len DataGridView  
        }

        private void binding()
        {
            txtmalop.DataBindings.Clear();
            txtmalop.DataBindings.Add("Text", grvclass.DataSource, "MaLop");
            txttenlop.DataBindings.Clear();
            txttenlop.DataBindings.Add("Text", grvclass.DataSource, "TenLop");
            txtcahoc.DataBindings.Clear();
            txtcahoc.DataBindings.Add("Text", grvclass.DataSource, "CaHoc");
            txtsiso.DataBindings.Clear();
            txtsiso.DataBindings.Add("Text", grvclass.DataSource, "SiSo");
        }

        public frmclass()
        {
            InitializeComponent();
        }

        private void frmclass_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            binding();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
        }

        private void cmdthemmoi_Click(object sender, EventArgs e)
        {
            frmclassthem _frmclassthem = new frmclassthem();
            _frmclassthem.Show();
            Hide();
        }

        private bool kiemtra()
        {
            kt = true;
            int i;
            if (txttenlop.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tên lớp học !", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
            }
            else if (Int32.TryParse((txtsiso.Text), out i) == false)
            {
                MessageBox.Show("Sĩ số phải là kiểu số !", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
            }
            return kt;
        }

        private void cmdsua_Click(object sender, EventArgs e)
        {
            if (txtmalop.Text.Length > 0)
            {
                kiemtra();
                if (kt == true)
                {
                    DataRow row = dtClass.Select("MaLop = " + Convert.ToInt32(txtmalop.Text))[0];
                    row.BeginEdit();
                    row["TenLop"] = txttenlop.Text;
                    row["CaHoc"] = txtcahoc.Text;
                    row["SiSo"] = txtsiso.Text;
                    row.EndEdit();
                    SqlCommand commandUpdate = new SqlCommand();
                    commandUpdate.Connection = con;
                    commandUpdate.CommandType = CommandType.Text;
                    commandUpdate.CommandText = @"Update tblClass Set fldClassName = @fldClassName, 
                                                                        fldTime=@fldTime, 
                                                                        fldCapacity=@fldCapacity
                                                                        Where fldID = @fldID";
                    commandUpdate.Parameters.Add("@fldID", SqlDbType.Int, 50, "MaLop");
                    commandUpdate.Parameters.Add("@fldClassName", SqlDbType.NVarChar, 50, "TenLop");
                    commandUpdate.Parameters.Add("@fldTime", SqlDbType.NVarChar, 50, "CaHoc");
                    commandUpdate.Parameters.Add("@fldCapacity", SqlDbType.Int, 50, "SiSo");
                    da.UpdateCommand = commandUpdate;
                    da.Update(dtClass);
                    MessageBox.Show("Bạn đã sửa thành công !", "THÔNG BÁO", MessageBoxButtons.OK);
                }
            }
        }

        private void cmdxoa_Click(object sender, EventArgs e)
        {
            if (txtmalop.Text.Length > 0)
            {
                DataRow row = dtClass.Select("MaLop = " + Convert.ToInt32(txtmalop.Text))[0];
                row.BeginEdit();
                row.Delete();
                row.EndEdit();
                SqlCommand commandDelete = new SqlCommand();
                commandDelete.Connection = con;
                commandDelete.CommandType = CommandType.Text;
                commandDelete.CommandText = "Delete From tblClass where fldID = @fldID";
                commandDelete.Parameters.Add("@fldID", SqlDbType.Int, 50, "MaLop");
                da.DeleteCommand = commandDelete;
                da.Update(dtClass);
                MessageBox.Show("Bạn đã xóa thành công !", "THÔNG BÁO", MessageBoxButtons.OK);
            }
        }
    }
}
